﻿@{Html.RenderPartial("_ConfigScripts");}

<script src="https://gadget-doc-examples.googlecode.com/svn/trunk/opensocial-gadgets/popup.js"></script>
@*<script src="@(Logic.Constants.CurrentUrl)/Content/tasksIndexGadget.c.js"></script>*@
<script src="@(Logic.Constants.CurrentUrl)/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> 
<style>
  #main {
    margin: 0px;
    padding: 0px;
    padding-bottom:40px;
    font-size: small;
  }
  </style>
  
  <div id="main" style="display: none">
  </div>

  <div id="approval" style="display: none">
    <img src="https://gadget-doc-examples.googlecode.com/svn/trunk/images/new.gif">
    <a href="#" id="personalize">Personalize this gadget</a>
  </div>

  <div id="waiting" style="display: none">
    Please click
    <a href="#" id="approvaldone">I've approved access</a>
    once you've approved access to your data.
  </div>
  
   <script type="text/javascript">
    var prefs = gadgets.views.getParams();
    var contentUrl = prefs.url ? prefs.url : 'tasks/index';
    var encodeValues = gadgets.io.encodeValues;
    function showOneSection(toshow)
    {
        var sections = ['main', 'approval', 'waiting'];
        for (var i = 0; i < sections.length; ++i)
        {
            var s = sections[i];
            var el = document.getElementById(s);
            if (s === toshow)
            {
                el.style.display = "block";
            } else
            {
                el.style.display = "none";
            }
        }
    }

    function fetchData(endPoint, bringData)
    {
        var params = {};
        var url = remoteUrl + endPoint + "?i=" + Math.random();
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
        params["OAUTH_SERVICE_NAME"] = "HMAC";
        params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;

        gadgets.io.makeRequest(url, function (response)
        {
            if (response.data && response.data.RedirectUrl)
                HandleLogin(response.data.RedirectUrl);
            else if (response.text)
            {
                if (endPoint == 'login')
                {
                    if (bringData)
                        fetchData(contentUrl);
                }
                else
                {
                    showOneSection('main');
                    $('#main').html($(response.text));
                    gadgets.window.adjustHeight(500);
                }
            }
            else
                ShowDebug(response);
        }, params);
    }

    function HandleLogin(url)
    {
        var popup = shindig.oauth.popup({
            destination: url,
            windowOptions: "width=350,height=250",
            onOpen: function () { showOneSection('waiting'); },
            onClose: function () { fetchData('login',true); }
        });

        var personalize = document.getElementById('personalize');
        personalize.onclick = popup.createOpenerOnClick();
        var approvaldone = document.getElementById('approvaldone');
        approvaldone.onclick = popup.createApprovedOnClick();
        showOneSection('approval');
    }

    function ShowDebug(response)
    {
        // The response.oauthError and response.oauthErrorText values may help debug
        // problems with your gadget.
        var main = document.getElementById('main');
        var err = document.createTextNode('OAuth error: ' +
        response.oauthError + ': ' + response.oauthErrorText);
        main.appendChild(err);
        showOneSection('main');
    }

    gadgets.util.registerOnLoadHandler(function () { fetchData('login', true) });
    window.setInterval(function () { fetchData('login', false); }, 300000);

    function remoteRequest(options)
    {
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = options.contentType;
        params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
        params["OAUTH_SERVICE_NAME"] = "HMAC";
        params[gadgets.io.RequestParameters.METHOD] = options.type;

        var data = options.data;
        if (data)
        {
            if (typeof data != "string")
                data = gadgets.io.encodeValues(data);
            params[gadgets.io.RequestParameters.POST_DATA] = data;
        }

        gadgets.io.makeRequest(options.url, function (res)
        {
            if (res && res.data && res.data.RedirectUrl)
                HandleLogin(res.data.RedirectUrl);

            if (options.success)
            {
                if (options.contentType == "JSON")
                {
                    if (res.data)
                    {
                        options.success(res.data);
                        //gadgets.window.adjustHeight();
                    }
                    else
                    {
                        alert("Error")
                    }
                }
                else options.success(res.text);
            }
        }, params);
    }
            
    function viewTaskInClarizen(id, url)
    {
        if(!url)
            var url = id ?
            stringFormat("/Clarizen/View/?id={0}&type={1}", id, "Task"):
            "/Clarizen/Pages/MainPage/WorkItems.aspx";
                
        remoteRequest({
            url: "@(Logic.Constants.CurrentUrl)/Clarizen/GetLink",
            contentType: "JSON",
            type: "POST",
            data: {link: escape(target)},
            success: function (data)
            {
                window.open(data);
            }
        });
    }

    function ChangeSize(){} 
  </script>